package com.dog.basics.security.util;

import cn.hutool.core.util.StrUtil;
import com.dog.basics.common.constant.CommonConstants;
import com.dog.basics.common.constant.SecurityConstants;
import com.dog.basics.security.exception.UnauthorizedExceptionBase;
import com.dog.basics.security.service.DogSysUser;
import com.dog.basics.security.service.DogUser;
import lombok.experimental.UtilityClass;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.PatternMatchUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 安全工具类
 *
 * @author L.cm
 */
@UtilityClass
public class SecurityUtils {
    /**
     * 获取Authentication
     */
    public Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * 获取用户
     */
    public DogUser getUser() {
        Authentication authentication = getAuthentication();
        Object principal = authentication.getPrincipal();
        if (principal instanceof DogUser) {
            return (DogUser) principal;
        }
        throw new UnauthorizedExceptionBase("无法获取普通用户信息");
    }

    /**
     * 获取系统用户
     */
    public DogSysUser getSysUser() {
        Authentication authentication = getAuthentication();
        Object principal = authentication.getPrincipal();
        if (principal instanceof DogSysUser) {
            return (DogSysUser) principal;
        }
        throw new UnauthorizedExceptionBase("无法获取系统用户信息");
    }

    /**
     * @功能描述: 判断是是否某某角色
     * @author He
     * @date 2020/4/20
     */
    public boolean hasRole(String hasRole) {
        if (StrUtil.isBlank(hasRole)) {
            return false;
        }
        hasRole = StrUtil.format("{}{}", CommonConstants.ROLE_PREFIX, hasRole);
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            return false;
        }
        String finalHasRole = hasRole;
        return authentication
                .getAuthorities()
                .stream()
                .map(GrantedAuthority::getAuthority)
                .filter(StringUtils::hasText)
                .anyMatch(x -> PatternMatchUtils.simpleMatch(finalHasRole, x));
    }
    /**
     * 获取用户角色信息
     * @return 角色集合
     */
    public List<Integer> getRoles() {
        Authentication authentication = getAuthentication();
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();

        List<Integer> roleIds = new ArrayList<>();
        authorities.stream().filter(granted -> StrUtil.startWith(granted.getAuthority(), CommonConstants.ROLE_PREFIX))
                .forEach(granted -> {
                    String id = StrUtil.removePrefix(granted.getAuthority(), CommonConstants.ROLE_PREFIX);
                    roleIds.add(Integer.parseInt(id));
                });
        return roleIds;
    }

}
